﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

using DM_Article.Model;

namespace DM_Article.Bll
{
    public class smallclassbll
    {
        public static int add(SqlConnection conn, smallclass model)
        {
            SqlCommand cmd = new SqlCommand("insert into smallclass(bigclassid,smallclassname)values('" + model.bigclassid + "','" + model.smallclassname + "')", conn);
            object obj = cmd.ExecuteNonQuery();
            if (obj == null)
            {
                return 1;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        public static int update(SqlConnection conn, smallclass model)
        {
            SqlCommand cmd = new SqlCommand("update smallclass set bigclassid='" + model.bigclassid + "',smallclassname ='" + model.smallclassname + "' where smallclassid='" + model.smallclassid + "'", conn);
            return cmd.ExecuteNonQuery();
        }

        public static int delete(SqlConnection conn, string smallclassid)
        {
            SqlCommand cmd = new SqlCommand("delete smallclass where smallclassid='" + smallclassid + "'", conn);
            return cmd.ExecuteNonQuery();
        }

        public static int deleteByBigclass(SqlConnection conn, string bigclassid)
        {
            SqlCommand cmd = new SqlCommand("delete smallclass where bigclassid='" + bigclassid + "'", conn);
            return cmd.ExecuteNonQuery();
        }

        public static smallclass getSmallclassDetail(SqlConnection conn, string smallclassid)
        {
            SqlDataAdapter da = new SqlDataAdapter("select [bigclassid],[smallclassname] from smallclass where smallclassid='" + smallclassid + "'", conn);
            DataTable dt = new DataTable();
            smallclass model = new smallclass();
            da.Fill(dt);
            if (dt != null && dt.Rows.Count > 0)
            {
                if (dt.Rows[0]["bigclassid"].ToString() != "")
                {
                    model.bigclassid = Convert.ToInt32(dt.Rows[0]["bigclassid"].ToString());
                }
                if (dt.Rows[0]["smallclassname"].ToString() != "")
                {
                    model.smallclassname = dt.Rows[0]["smallclassname"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }

        }

        public static DataTable getSmallclassTable(SqlConnection conn)
        {
            SqlDataAdapter da = new SqlDataAdapter("select [smallclassid],[bigclassid],[smallclassname] from smallclass", conn);
            DataTable dt = new DataTable();
            da.Fill(dt);
            return dt;
        }

        public static DataTable getSmallByBig(SqlConnection conn, int bigclassid)
        {
            SqlDataAdapter da = new SqlDataAdapter("select [smallclassid],[smallclassname] from smallclass where bigclassid='" + bigclassid + "'", conn);
            DataTable dt = new DataTable();
            da.Fill(dt);
            return dt;
        }
    }
}
